package com.hope.hotel.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hope.hotel.entity.po.Review;
import com.hope.hotel.entity.vo.ReviewVO;
import org.apache.ibatis.annotations.*;

import java.util.List;
import java.util.Map;

/**
 * 评论数据访问接口
 */
@Mapper
public interface ReviewMapper extends BaseMapper<Review> {
    
    /**
     * 获取指定酒店的所有评论
     * @param hotelId 酒店ID
     * @return 评论列表
     */
    @Select("SELECT r.*, u.username as user_name, u.avatar as user_avatar " +
            "FROM reviews r " +
            "JOIN users u ON r.user_id = u.user_id " +
            "WHERE r.hotel_id = #{hotelId} AND r.status != 3 " +
            "ORDER BY r.created_at DESC")
    List<Map<String, Object>> getHotelReviews(Long hotelId);

    /**
     * 分页获取酒店评论
     * @param page 分页参数
     * @param hotelId 酒店ID
     * @param rating 评分筛选(可选)
     * @param status 状态筛选(可选)
     * @param startDate 开始日期(可选)
     * @param endDate 结束日期(可选)
     * @return 分页结果
     */
    @Select({
            "<script>",
            "SELECT r.*, u.username as user_name, u.avatar as user_avatar ",
            "FROM reviews r ",
            "JOIN users u ON r.user_id = u.user_id ",
            "WHERE r.hotel_id = #{hotelId} AND r.status != 3 ",
            "<if test='rating != null'>",
            "  AND r.rating = #{rating} ",
            "</if>",
            "<if test='status != null'>",
            "  AND r.status = #{status} ",
            "</if>",
            "<if test='startDate != null and endDate != null'>",
            "  AND r.created_at BETWEEN #{startDate} AND #{endDate} ",
            "</if>",
            "ORDER BY r.created_at DESC",
            "</script>"
    })
    IPage<Map<String, Object>> getHotelReviewsPage(Page<?> page, Long hotelId, Integer rating, 
                                                 Integer status, String startDate, String endDate);
    
    /**
     * 获取评论详情
     * @param reviewId 评论ID
     * @return 评论详情
     */
    @Select("SELECT r.*, u.username as user_name, u.avatar as user_avatar " +
            "FROM reviews r " +
            "JOIN users u ON r.user_id = u.user_id " +
            "WHERE r.review_id = #{reviewId}")
    Map<String, Object> getReviewDetail(Long reviewId);
    
    /**
     * 更新评论回复
     * @param reviewId 评论ID
     * @param reply 回复内容
     * @return 受影响行数
     */
    @Update("UPDATE reviews SET reply = #{reply}, reply_time = NOW(), " +
            "status = 1, updated_at = NOW() " +
            "WHERE review_id = #{reviewId}")
    int updateReviewReply(Long reviewId, String reply);
    
    /**
     * 更新评论状态
     * @param reviewId 评论ID
     * @param status 状态码
     * @return 受影响行数
     */
    @Update("UPDATE reviews SET status = #{status}, updated_at = NOW() " +
            "WHERE review_id = #{reviewId}")
    int updateReviewStatus(Long reviewId, Integer status);
    
    /**
     * 获取评论统计信息
     * @param hotelId 酒店ID
     * @return 统计信息
     */
    @Select("SELECT " +
            "COUNT(*) as total_reviews, " +
            "AVG(rating) as avg_rating, " +
            "SUM(CASE WHEN status = 1 THEN 1 ELSE 0 END) as replied_count, " +
            "SUM(CASE WHEN rating = 5 THEN 1 ELSE 0 END) as rating_5, " +
            "SUM(CASE WHEN rating = 4 THEN 1 ELSE 0 END) as rating_4, " +
            "SUM(CASE WHEN rating = 3 THEN 1 ELSE 0 END) as rating_3, " +
            "SUM(CASE WHEN rating = 2 THEN 1 ELSE 0 END) as rating_2, " +
            "SUM(CASE WHEN rating = 1 THEN 1 ELSE 0 END) as rating_1 " +
            "FROM reviews " +
            "WHERE hotel_id = #{hotelId} AND status != 3")
    Map<String, Object> getReviewStats(Long hotelId);
    
    /**
     * 检查用户是否已评论过该订单
     * @param bookingId 订单ID
     * @param userId 用户ID
     * @return 评论数量
     */
    @Select("SELECT COUNT(*) FROM reviews WHERE booking_id = #{bookingId} AND user_id = #{userId}")
    Integer countBookingReviews(Long bookingId, Long userId);
} 